package com.app.business.utils;

import com.app.business.model.Contract;
import com.app.business.service.ContractService;
import com.app.business.service.HouseService;
import com.app.business.service.OverAreaPriceNewService;
import com.app.system.utils.ApplicationContextProvider;
import com.app.system.utils.dataType.BigDecimalUtils;
import com.google.common.collect.Maps;
import org.springframework.context.ApplicationContext;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 对不同面积区间单价进行封装
 * Created by ddz on 2021/11/5.
 */
public class AreaAndPriceUtils {
    private static ContractService contractService;
    private static OverAreaPriceNewService overAreaPriceNewService;
    private static HouseService houseService;

    public static List<Map> getAreaAndPriceList(Contract contract) {
        ApplicationContext applicationContext = ApplicationContextProvider.getApplicationContext();
        contractService = (ContractService) applicationContext.getBean(ContractService.class);
        overAreaPriceNewService = (OverAreaPriceNewService) applicationContext.getBean(OverAreaPriceNewService.class);
        houseService = (HouseService) applicationContext.getBean(HouseService.class);

        Map<String, String> areaAndPriceMap = contractService.getAreaAndPriceMap(contract.getId());
        Map<String, Map> overHouseMoneyMap = overAreaPriceNewService.getOverHouseMoneyMapForMore(contract);

        BigDecimal noSpfArea = houseService.getNoSpfArea(contract.getId());


        List<Map> areaList = new ArrayList<>();
        Map maps = Maps.newHashMap();
        //获取产权调换面积及单价
        BigDecimal syArea = BigDecimal.ZERO;
        BigDecimal cqArea = BigDecimal.ZERO;
        BigDecimal propertyChangeArea = BigDecimalUtils.getBigAndBigSub(contract.getPropertyChangeArea(),contract.getKcPropertyChangeArea()) == null ? BigDecimal.ZERO : BigDecimalUtils.getBigAndBigSub(contract.getPropertyChangeArea(),contract.getKcPropertyChangeArea());
        BigDecimal chooseHouseAreaTotal = noSpfArea == null ? BigDecimal.ZERO : noSpfArea;
        if (propertyChangeArea.compareTo(chooseHouseAreaTotal) > 0) {
            cqArea = noSpfArea;
        } else {
            cqArea = BigDecimalUtils.getBigAndBigSub(contract.getPropertyChangeArea(),contract.getKcPropertyChangeArea());
        }
        maps.put("project", "产权调换面积");
        maps.put("area", cqArea);
        maps.put("price", contract.getPropertyChangeAreaPrice() == null ? BigDecimal.ZERO : contract.getPropertyChangeAreaPrice());
        maps.put("money", BigDecimalUtils.getBigAndBigMul(cqArea, contract.getPropertyChangeAreaPrice()));
        maps.put("bl", BigDecimalUtils.getBigPercentBigDecimal(cqArea, noSpfArea));
        maps.put("memo", "");
        areaList.add(maps);
        syArea = BigDecimalUtils.getBigAndBigSub(noSpfArea, cqArea);
        //获取照顾购买面积及单价
        if (syArea.compareTo(BigDecimal.ZERO) > 0) {
            BigDecimal careBuyArea = BigDecimal.ZERO;
            BigDecimal careBuyAreas = BigDecimal.ZERO;
            BigDecimal careBuyAreaPrice = BigDecimal.ZERO;
            for (String key : areaAndPriceMap.keySet()) {
                maps = Maps.newHashMap();
                careBuyAreas = BigDecimalUtils.stringToBigDecimal(String.valueOf(areaAndPriceMap.get(key)));
                careBuyAreaPrice = BigDecimalUtils.stringToBigDecimal(String.valueOf(key));
                if (syArea.compareTo(careBuyAreas) > 0) {
                    careBuyArea = careBuyAreas;
                } else {
                    careBuyArea = syArea;
                }
                maps.put("project", "照顾购买" + careBuyAreaPrice);
                maps.put("area", careBuyArea);
                maps.put("price", careBuyAreaPrice);
                maps.put("money", BigDecimalUtils.getBigAndBigMul(careBuyArea, careBuyAreaPrice));
                maps.put("bl", BigDecimalUtils.getBigPercentBigDecimal(careBuyArea, noSpfArea));
                maps.put("memo", "");

                areaList.add(maps);
                syArea = BigDecimalUtils.getBigAndBigSub(syArea, careBuyArea);
            }
        }
        //获取扩购面积及单价
        if (syArea.compareTo(BigDecimal.ZERO) > 0) {
            BigDecimal kgArea = BigDecimal.ZERO;
            BigDecimal kgPrice = BigDecimal.ZERO;
            for (String key : overHouseMoneyMap.keySet()) {
                maps = Maps.newHashMap();
                kgArea = BigDecimalUtils.stringToBigDecimal(String.valueOf(overHouseMoneyMap.get(key).get("area")));
                kgPrice = BigDecimalUtils.stringToBigDecimal(String.valueOf(overHouseMoneyMap.get(key).get("price")));
                if (kgArea.compareTo(BigDecimal.ZERO) > 0) {
                    maps.put("project", "扩购" + kgPrice);
                    maps.put("area", kgArea);
                    maps.put("price", kgPrice);
                    maps.put("money", BigDecimalUtils.getBigAndBigMul(kgArea, kgPrice));
                    maps.put("bl", BigDecimalUtils.getBigPercentBigDecimal(kgArea, noSpfArea));
                    maps.put("memo", "");
                    areaList.add(maps);
                }
            }
        }

        return areaList;
    }

}